'''
Copyright 2013 Matthew Ramina

This file is part of The Time Machine.

    The Time Machine is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    The Time Machine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with The Time Machine.  If not, see <http://www.gnu.org/licenses/>.
'''
import sys
from PyQt4 import QtGui, QtCore
from window.gui import Ui_MainWindow
from engine.game import Game

class MyWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.lineEdit, QtCore.SIGNAL("returnPressed()"), self.command)
        QtCore.QTimer.singleShot(0, self.start)
        self.newCommand = False
        self.input = ""

    def command(self):
        text = self.ui.lineEdit.text()
        self.ui.lineEdit.clear()
        self.write("> %s" % text)
        parsed = play.parser(text)
        nextCommand = play.nextCommand(parsed)
        self.write(nextCommand)

    def changePic(self, pic):
        myPixmap = QtGui.QPixmap("pics/%s" % pic)
        myScaledPixmap = myPixmap.scaled(self.ui.picLabel.size(), QtCore.Qt.KeepAspectRatio)
        self.ui.picLabel.setPixmap(myScaledPixmap)

    def write(self, text):
        self.ui.textDisplay.append(text)

    def start(self):
        self.write(play.intro)
        coords = play.searchText()
        for (index, thing) in enumerate(coords):
            self.write(str(index + 1) + ": " + thing)


        # store all rooms in dict
        # ???
        # go to starting room
        # PROFIT!!1!

        # TODO: Game loop
        # display room description
        # change pic
        # display question/prompt
        # parse answer/add score
        # ???
        # advance room
        # PROFIT!!1!

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyWindow()
    myapp.show()
    myapp.changePic("pie.jpg")
    play = Game()
    sys.exit(app.exec_())
